package kr.co.doriclassic.mypuppystory.alarmpuppy;

import kr.co.doriclassic.mypuppystory.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
	TextView title;
	TextView date;
	View v;
	TextView content;
	SharedPreferences pref;
	boolean flag = false;
	private int id;
	private Bundle bundle;
	private final static long[] vibrates = new long[] { 1000, 1000, 500, 500,
			200, 200, 200, 200, 200, 200 };

	@Override
	public void onReceive(final Context context, Intent intent) {
		bundle = intent.getExtras();
		id = bundle.getInt("reqCode");
		pref = context.getSharedPreferences("saveDate", Activity.MODE_PRIVATE);
		v = View.inflate(context, R.layout.customtoast, null);

		toast(context);
		noti(context);

	}// onReceive

	private void toast(Context context) {
		title = (TextView) v.findViewById(R.id.title);
		date = (TextView) v.findViewById(R.id.date);
		content = (TextView) v.findViewById(R.id.content);
		v.setBackgroundResource(R.drawable.bluestom_toast);
		title.setText(pref.getString("title" + id, ""));
		date.setText("예정일 : " + pref.getString("date" + id, ""));
		content.setText("가까운 동물병원을\n 방문해주세요~~.");
		Toast toast = new Toast(context);
		toast.setView(v);
		int xOffset = 0;
		int yOffset = 100;
		toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, xOffset,
				yOffset);
		toast.setDuration(Toast.LENGTH_SHORT);
		ToastAll(context, toast);

	}

	// 토스트 중복방지를 위해 설정 토스트가 안보일때 false 보일때 true 기본설정은 당연히 false

	private void noti(Context context) {
		
		NotificationManager mNotiManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		
		
		Intent newIntent = new Intent(context, AlarmStop.class);
		// newIntent.putExtra("intent_id", id);
		newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pIntent = PendingIntent.getActivity(context, id,
				newIntent, 0);

		Notification noti = new Notification.Builder(context)
				.setContentTitle(pref.getString("title" + id, ""))
				.setContentText("예정일 : " + pref.getString("date" + id, ""))
				.setSubText("가까운 동물병원을 방문해주세요~~.")
				.setSmallIcon(R.drawable.jusa)
				.setLargeIcon(
						BitmapFactory.decodeResource(context.getResources(),
								R.drawable.jusa))
				.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
				.setContentIntent(pIntent)
				.build();
		noti.flags = Notification.FLAG_AUTO_CANCEL;
		mNotiManager.notify(id, noti);
	}

	public void ToastAll(Context context, Toast mToast) {

		if (flag == false) {
			flag = true;
			mToast.show();
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					flag = false;
				}
			}, 2000);// 토스트 켜져있는 시간동안 핸들러 지연 대충 숏이 2초 조금 넘는거 같다.
		} else {
		}
	}// ToastAll
}// class
